package com.java.imauoj.controler;

import com.java.imauoj.Tools.Function;
import com.java.imauoj.moudel.*;
import com.java.imauoj.sercive.SubmitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("api/v1")
public class SubmitControler {

    @Autowired
    private SubmitService service;

    @PostMapping("submitCode")
    public ResultVo<Solution> submitCode(@RequestBody Solution solution, HttpSession session){
        ResultVo re = new ResultVo();
        User userLogin = (User)session.getAttribute("login");
        if(userLogin!=null){
            Role role = (Role)session.getAttribute("role");
            if(role.getIs_submit_code() == 0){
                re.setStatusCode(0);
                re.setMessage("没有权限");
            }else{
                re.setStatusCode(1);
                re.setMessage("成功");
                service.addSolution(solution);
                service.addSourceCode(solution.getSolution_id(),solution.getText());
                Solution solution1 = service.getResult(solution.getSolution_id());
                re.setData(solution1);
            }
        }
        return re;
    }

    @PostMapping("getSolutionList")
    public ResultVo<List<Solution>> getSolutionList(@RequestBody Filter filter, HttpSession session){
        ResultVo re = new ResultVo();
        User userLogin = (User)session.getAttribute("login");
        if(userLogin!=null){
            Role role = (Role)session.getAttribute("role");
            if(role.getIs_get_solution() == 0){
                re.setStatusCode(0);
                re.setMessage("没有权限");
            }else{
                re.setStatusCode(1);
                re.setMessage("成功");
                Function function = new Function();
                FanHuiZhi fhz = function.qinjie(filter);
                re.setData(service.getSolutionList(fhz));
            }
        }
        return re;

    }
}
